# https://ukdevguy.com/how-to-create-a-game-window-in-pygame/

import pygame
import sys

pygame.init()
winx = 800
winy = 400
# Create a window
screen = pygame.display.set_mode((winx, winy))
screen.fill((0, 0, 0))
GOLD = 255, 251, 0
RED = pygame.Color('red')
x = 300  # 左上角坐标
y = 150
w = 50  # 宽高
h = 50

# r2rect = pygame.draw.rect(screen,RED,(210, 210, 200, 100), 0)

# Set title
pygame.display.set_caption('new pygame')
fclock = pygame.time.Clock()
j = 1
sx = 5  # x 方向速度
sy = 10  # y 方向速度
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    if x >= winx - w:  # 如果到右边的边上了,
        sx *= -1
    if y >= winy - h:  # 如果到下边的边上了,
        sy *= -1
    if x <= 0:  # 如果到左边的边上了,
        sx *= -1
    if y <= 0:  # 如果到上边的边上了,
        sy *= -1
    x += sx
    y += sy
    r1 = x, y, w, h
    print(r1, j)
    screen.fill((0, 0, 0))
    pygame.draw.rect(screen, GOLD, r1, 0)
    pygame.display.update()
    fclock.tick(30)
